package de.zainodis.triplog.ui;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import de.zainodis.triplog.R;
import de.zainodis.triplog.data.WeatherEntry;
import de.zainodis.triplog.ui.fragment.ForecastFragment;
import de.zainodis.triplog.util.DateTimeUtils;
import de.zainodis.triplog.util.Utilities;

/**
 * Created by fzarrai on 8/11/2014.
 */
public class WeatherForecastAdapter extends CursorAdapter {

    private static final String TAG = "WeatherForecastAdapter";
    private static final int VIEW_TYPE_TODAY = 0;
    private static final int VIEW_TYPE_FUTURE = 1;
    private boolean isDualPane;

    public WeatherForecastAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
    }

    public void setIsDualPane(boolean isDualPane) {
        this.isDualPane = isDualPane;
    }

    public boolean isDualPane() {
        return isDualPane;
    }

    @Override
    public int getViewTypeCount() {
        //Since we're using two different views
        return 2;
    }

    @Override
    public int getItemViewType(int position) {

        if (isDualPane) {
            return VIEW_TYPE_FUTURE;
        }
        return position == 0 ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int viewType = getItemViewType(cursor.getPosition());
        int layoutId = R.layout.list_item_forecast;
        switch (viewType) {
            case 0:
                layoutId = R.layout.list_item_forecast_today;
                break;
        }

        View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
        return view;
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder viewHolder = (ViewHolder) view.getTag();

        int weatherId = cursor.getInt(ForecastFragment.COLUMN_ID);
        String max = cursor.getString(ForecastFragment.COLUMN_MAX_TEMP);
        String min = cursor.getString(ForecastFragment.COLUMN_MIN_TEMP);
        String description = cursor.getString(ForecastFragment.COLUMN_DESCRIPTION);
        String date = cursor.getString(ForecastFragment.COLUMN_DATE);
        int iconId = cursor.getInt(ForecastFragment.COLUMN_ICON_ID);

        WeatherEntry entry = new WeatherEntry();
        entry.setMax(Integer.valueOf(max));
        entry.setMin(Integer.valueOf(min));
        entry.setDescription(description);
        entry.setDate(date);
        entry.setIconId(iconId);
        populateView(entry, view, context, viewHolder);

    }

    private static class ViewHolder {
        public final TextView dateView;
        public final ImageView iconView;
        public final TextView summaryView;
        public final TextView minView;
        public final TextView maxView;

        public ViewHolder(View view) {
            dateView = (TextView) view.findViewById(R.id.list_item_forecast_date);
            summaryView = (TextView) view.findViewById(R.id.list_item_forecast_summary);
            minView = (TextView) view.findViewById(R.id.list_item_forecast_min);
            maxView = (TextView) view.findViewById(R.id.list_item_forecast_max);
            iconView = (ImageView) view.findViewById(R.id.list_item_forecast_image);
        }
    }

    /**
     * If you reuse the layout of this adapter you can call this method to populate the view.
     *
     * @param rootView     root view of the list_item_forecast.xml layout.
     * @param weatherEntry the weather entry to display
     * @param context      Context required to resolve certain values.
     * @param viewHolder   you can pass null here if you don't have a viewholder.
     */
    public static void populateView(WeatherEntry weatherEntry, View rootView, Context context, ViewHolder viewHolder) {
        if (weatherEntry == null) {
            return;
        }

        if (viewHolder == null) {
            viewHolder = new ViewHolder(rootView);
        }
        String date = DateTimeUtils.getFriendlyDayString(context, weatherEntry.getDate());
        viewHolder.dateView.setText(date);

        String description = weatherEntry.getDescription();
        viewHolder.summaryView.setText(description);

        String unit = Utilities.getPreferredUnit(context);
        boolean isMetric = context.getString(R.string.pref_value_metric).equals(unit);
        String min = Utilities.formatTemperature(context, Integer.valueOf(weatherEntry.getMin()), isMetric);
        String max = Utilities.formatTemperature(context, Integer.valueOf(weatherEntry.getMax()), isMetric);

        viewHolder.minView.setText(min);
        viewHolder.maxView.setText(max);

        int owmId = weatherEntry.getIconId();
        int id = Utilities.getWeatherIconId(owmId);
        if (id > 0) {
            viewHolder.iconView.setImageDrawable(context.getResources().getDrawable(id));
            viewHolder.iconView.setContentDescription(String.format("%s, %s, %s to %s", date, description, min, max));
        } else {
            Log.e(TAG, "Failed to find matching resource id for osm id " + owmId);
        }
    }
}

